<?php


class Auth_carApp extends BackendApp
{
    var $_auth_car_mod;

    function __construct()
    {
        $this->Auth_carApp();
    }

    function Auth_carApp()
    {
        parent::BackendApp();
        $this->_auth_car_mod =& m('auth_car');
    }

    function index()
    {
        $db =& db();
        $sql = "SELECT d_key,d_value FROM zys_dictionary WHERE d_code='authstatus'";
        $types = array();
        $temp = $db->getAll($sql);
        foreach ($temp as $v) {
            $types[$v['d_key']] = $v['d_value'];
        }
        $types[''] = '全部';
        $this->assign('types', $types);

        $conditions = "1=1 ";
        if ($_GET['type_id'] != '') {
            $conditions .= " and ac.a_authed=" . intval($_GET['type_id']);
        }
        if($_GET['a_vhc_code']!=''){
            $conditions.=" and a.a_vhc_code like '%".$_GET['a_vhc_code']."%'";
        }
        if($_GET['a_username']!=''){
            $conditions.=" and m.user_name like '%".$_GET['a_username']."%'";
        }
        $page = $this->_get_page(10);   //获取分页信息
        $sql = "select ac.*,m.user_name,m1.user_name as opname,a.a_type,a.a_vhc_code,a.a_vhc_model,a.a_vhc_size,a.a_vhc_weight from zys_auth_car ac
        LEFT JOIN zys_member m ON ac.a_userid=m.user_id
        LEFT JOIN zys_member m1 ON ac.a_auth_opid=m1.user_id
        LEFT JOIN zys_auth a ON ac.a_tid=a.a_tid
        WHERE $conditions ORDER BY ac.a_auth_date DESC limit $page[limit]";
        $auth_car = $this->_auth_car_mod->getAll($sql);
        $sql = "select COUNT(*) from zys_auth_car ac
        LEFT JOIN zys_member m ON ac.a_userid=m.user_id
        LEFT JOIN zys_auth a ON ac.a_tid=a.a_tid
        WHERE  $conditions ORDER BY ac.a_auth_date DESC ";
        $page['item_count'] = $this->_auth_car_mod->getOne($sql);   //获取统计数据
        $this->_format_page($page);
        $this->assign('page_info', $page);   //将分页信息传递给视图，用于形成分页条
        $this->assign('auth', $auth_car);
        $this->import_resource(array('script' => 'inline_edit.js,jquery.ui/jquery.ui.js,jquery.ui/i18n/' . i18n_code() . '.js',
            'style' => 'jquery.ui/themes/ui-lightness/jquery.ui.css'));

        $this->display('auth_car.index.html');
    }

    function pics()
    {
        $id = empty($_GET['id']) ? 0 : intval($_GET['id']);
        if ($id == 0) {
            $this->show_warning('用户不存在', '', 'index.php?app=auth_car');
            return;
        }
        $db =& db();
        $sql = "SELECT * FROM zys_auth_car_pic WHERE ap_a_tid=" . $id;
        $pics = $db->getAll($sql);
        $this->assign('pics', $pics);
        $this->display('auth_car.pics.html');
    }


    //删除
    function drop()
    {

        $arr = isset($_GET['id']) ? trim($_GET['id']) : '';

        $arrs = explode(',', $arr);


        if ($arrs && count($arrs) > 0) {
            foreach ($arrs as $v) {
                $temp = $this->_auth_car_mod->get($v);
                if (empty($temp['a_authed'])) {
                    $this->_auth_car_mod->drop($v);
                }
            }
        }

        $this->show_message('删除成功', '', 'index.php?app=auth_car');

    }

    //认证
    function authentication()
    {

        $arr = isset($_GET['id']) ? trim($_GET['id']) : '';
        $arrs = explode(',', $arr);
        $db =&db();
        if ($arrs && count($arrs) > 0) {
            foreach ($arrs as $v) {
                $this->_auth_car_mod->edit(" a_actid=$v and a_authed=0 ", array(
                    'a_authed' => 1,
                    'a_auth_opid' => $this->visitor->get('user_id'),
                    'a_auth_date' => gmtime()
                ));

                $sql="select a.*,m.user_name,m.phone_tel from zys_auth_car ac left join zys_auth a on ac.a_tid = a.a_tid left join zys_member m on a.a_userid = m.user_id where ac.a_actid=".$v;
                $res=$db->getRow($sql);

                if($this->checkUrl('http://192.168.0.180:8074/PlantFormService.asmx?WSDL')) {
                    $client = new soapclient('http://192.168.0.180:8074/PlantFormService.asmx?WSDL');
                    $client->soap_defencoding = 'utf-8';
                    $client->decode_utf8 = false;
                    $client->xml_encoding = 'utf-8';
                    $param = array('PJson'=>'{"CarNo": "'.$res['a_vhc_code'].'","CarType": "'.$res['a_vhc_model'].'","CarDriver": "'.$res['user_name'].'",
                        "DriverTel": "'.$res['phone_tel'].'","CarLenth": "'.$res['a_vhc_size'].'","CarLoad": "'.$res['a_vhc_weight'].'"}');

                    $result = $client->__Call("SetCarNo", array($param));
                    if (is_soap_fault($result)) {
                        trigger_error("SOAP Fault: (faultcode: {$result->faultcode}, faultstring: {$result->faultstring})", E_USER_ERROR);
                    } else {
                        $result = json_decode(json_encode($result->SetCarNoResult), TRUE);
                        $result = json_decode($result, true);
                        if($result['status']!=1){
                            $this->show_warning('同步数据失败','','index.php?app=auth_car');
                            return;
                        }
                    }
                }else{
                    $this->show_warning('业务系统无法链接','','index.php?app=auth_car');
                    return;
                }
            }
        }

        $this->show_message('认证成功', '', 'index.php?app=auth_car');

    }

    //退回
    function refuse()
    {
        $db =& db();
        $arr = isset($_POST['id']) ? trim($_POST['id']) : '';
        $content = isset($_POST['content']) ? trim($_POST['content']) : '';
        $arrs = explode(',', $arr);
        try {
            if ($arrs && count($arrs) > 0) {
                foreach ($arrs as $v) {
                    $this->_auth_car_mod->edit(" a_actid=$v and a_authed=0 ", array(
                        'a_auth_content' => $content,
                        'a_authed' => 2,
                        'a_auth_opid' => $this->visitor->get('user_id'),
                        'a_auth_date' => gmtime()
                    ));
                }
            } else {
                if (!$this->_auth_car_mod->edit(" a_actid=$arr ", array('a_authed' => 2, 'a_auth_content' => $content, 'a_auth_opid' => $this->visitor->get('user_id'), 'a_auth_date' => gmtime()))) {
                    throw new Exception($this->_auth_car_mod->get_error());
                    return;
                }
            }
            $this->json_result('', '退回成功');
        } catch (Exception $e) {
            $this->json_error($e->getMessage());
            exit ();
        }
    }

}
